package simulateOS.UI;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.springframework.context.ApplicationContext;

import java.io.File;

import static javafx.stage.StageStyle.TRANSPARENT;

public class MainUI {
    private Label myComputer = new Label();/*电脑图标*/
    private Label myDisk = new Label();/*磁盘图标*/
    private Label myProgram = new Label();/*程序图标*/
    private Label shutdown = new Label();/*关机图标*/
    //依次设置图片

    private ClassLoader classLoader = MainUI.class.getClassLoader();
//    private File imagePath = new File("src\\main\\resources\\image");/*图片路径*/
    private Image MyComputer = new Image(classLoader.getResourceAsStream("image/MyComputer.png"), 60, 60, true, true);
    private Image MyDisk = new Image(classLoader.getResourceAsStream("image/MyDisk.png"), 60, 60, true, true);
    private Image MyProgram = new Image(classLoader.getResourceAsStream("image/MyProgram.png"), 60, 60, true, true);
    private Image Shutdown = new Image(classLoader.getResourceAsStream("image/Shutdown.png"), 60, 60, true, true);

    /**
     * 以下为主面板
     */
    private GridPane gridPane = new GridPane();

    private AnchorPane anchorPane = new AnchorPane();

    private Stage processStage = ProcessAndMemoryUI.getProcessStage();

    private Scene scene;
    private Stage stage = new Stage();
    private static MainUI mainUI = null;

    public static MainUI getMainUI() {
        if (mainUI == null) {
            return new MainUI();
        } else {
            return mainUI;
        }
    }

    private MainUI() {
        initGridPane();
        scene = new Scene(anchorPane);
        stage.setScene(scene);
        //去掉系统自带的标题栏
        stage.initStyle(TRANSPARENT);

        //事件监控
        myComputer.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.getClickCount() == 2) {
                    FileAppUI.getFileAppUI().getStage().show();
                }
            }
        });

        myDisk.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.getClickCount() == 2) {
                    DiskUI.getDiskUI().getStage().show();
                }
            }
        });

        myProgram.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.getClickCount() == 2) {
                    /**
                     * 激活程序舞台
                     */
                    Stage processStage = ProcessAndMemoryUI.getProcessStage();
                    processStage.show();
                }
            }
        });

        shutdown.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.getClickCount() == 2) {
                    FileAppUI.getFileAppUI().getStage().close();
                    DiskUI.getDiskUI().getStage().close();
                    processStage.close();
                    /**
                     * 关闭程序舞台
                     */
                    stage.close();
                }
            }
        });
    }

    public Stage getStage() {
        return stage;
    }

    private void initGridPane() {


        VBox[] vBoxes = new VBox[3];
        vBoxes[0] = new VBox();
        vBoxes[1] = new VBox();
        vBoxes[2] = new VBox();
//        stage.setAlwaysOnTop(true);
//        stage.setFullScreen(true);
        //获取屏幕大小
        Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
        double width = screenRectangle.getWidth();
        double height = screenRectangle.getHeight();
        //设置窗口大小为屏幕大小除了下面任务栏的高度
        stage.setWidth(width);
        stage.setHeight(height - 40);


        //设置图标
        myComputer.setGraphic(new ImageView(MyComputer));
        myDisk.setGraphic(new ImageView(MyDisk));
        myProgram.setGraphic(new ImageView(MyProgram));
        shutdown.setGraphic(new ImageView(Shutdown));
        //设置text
        myComputer.setText("我的电脑");
        myDisk.setText("我的磁盘");
        myProgram.setText("我的程序");
        shutdown.setText("关机");
        //设置字体大小和颜色为白色和字体样式：微软雅黑体：Microsoft YaHei
        myComputer.setStyle("-fx-font-size: 20px;-fx-text-fill: white;-fx-font-family: 'Microsoft YaHei'");
        myDisk.setStyle("-fx-font-size: 20px;-fx-text-fill: white;-fx-font-family: 'Microsoft YaHei'");
        myProgram.setStyle("-fx-font-size: 20px;-fx-text-fill: white;-fx-font-family: 'Microsoft YaHei'");
        shutdown.setStyle("-fx-font-size: 20px;-fx-text-fill: white;-fx-font-family: 'Microsoft YaHei'");

        //设置text在图片底部
        myComputer.setContentDisplay(ContentDisplay.TOP);
        myDisk.setContentDisplay(ContentDisplay.TOP);
        myProgram.setContentDisplay(ContentDisplay.TOP);
        shutdown.setContentDisplay(ContentDisplay.LEFT);
        vBoxes[0].getChildren().addAll(myComputer);
        vBoxes[1].getChildren().addAll(myDisk);
        vBoxes[2].getChildren().addAll(myProgram);

        //将vBox添加到anchorPane中底部
        anchorPane.getChildren().addAll(vBoxes);
        AnchorPane.setBottomAnchor(vBoxes[0], 5.0);
        AnchorPane.setBottomAnchor(vBoxes[1], 5.0);
        AnchorPane.setBottomAnchor(vBoxes[2], 5.0);
        //vBoxes[1]底部中间
        AnchorPane.setLeftAnchor(vBoxes[0], width / 2 - 100);
        AnchorPane.setLeftAnchor(vBoxes[1], width / 2);
        AnchorPane.setLeftAnchor(vBoxes[2], width / 2 + 100);

        //shutdown按钮添加到右上角
        anchorPane.getChildren().add(shutdown);
        AnchorPane.setTopAnchor(shutdown, 30.0);
        AnchorPane.setRightAnchor(shutdown, 30.0);

        //以下为设置背景图片
        Image backgroundImage = new Image(classLoader.getResourceAsStream("image/10-14-Day-Thumb.jpg") , width, height-40, false, false);
        BackgroundSize size = new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false);
        BackgroundImage myBack = new BackgroundImage(backgroundImage, BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, size);
        Background background = new Background(myBack);

        anchorPane.setBackground(background);
    }
}
